package com.wasu.doicommon.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SharedPreferenceHelper {

    private static HashMap<String, SharedPreferenceHelper> sInstanceMap = null;

    private final SharedPreferences mSharedPreferences;

    public static SharedPreferenceHelper getInstance(Context context, String fileName) {
        return getInstance(context, fileName, Context.MODE_PRIVATE);

    }

    public static SharedPreferenceHelper getInstance(Context context, String fileName, int model) {
        synchronized (SharedPreferenceHelper.class) {
            if (sInstanceMap == null) {
                sInstanceMap = new HashMap<>();
            }
            SharedPreferenceHelper helper = sInstanceMap.get(fileName);
            if (helper == null) {
                helper = new SharedPreferenceHelper(context, fileName, model);
                sInstanceMap.put(fileName, helper);
            }
            return helper;
        }
    }

    private SharedPreferenceHelper(Context context, String fileName, int model) {
        mSharedPreferences = context.getSharedPreferences(fileName, model);
    }

    public float load(String key, float defValue) {
        return mSharedPreferences.getFloat(key, defValue);
    }

    public int load(String key, int defValue) {
        return mSharedPreferences.getInt(key, defValue);
    }

    public long load(String key, long defValue) {
        return mSharedPreferences.getLong(key, defValue);
    }

    public String load(String key, String defValue) {
        return mSharedPreferences.getString(key, defValue);
    }

    public boolean load(String key, boolean paramBoolean) {
        return mSharedPreferences.getBoolean(key, paramBoolean);
    }

    public Map<String, ?> loadAll(String key) {
        return mSharedPreferences.getAll();
    }

    public boolean loadBoolean(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    public float loadFloat(String key) {
        return mSharedPreferences.getFloat(key, 0.0F);
    }

    public int loadInt(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    public long loadLong(String paramString) {
        return mSharedPreferences.getLong(paramString, 0L);
    }

    public Set<String> loadStringSet(String key) {
        return mSharedPreferences.getStringSet(key, null);
    }

    public String loadString(String key) {
        try {
            return mSharedPreferences.getString(key, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean removeAllKey() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.clear();
        return editor.commit();
    }

    public boolean removeKey(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        return editor.commit();
    }

    public boolean save(String key, float value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    public boolean save(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public boolean save(String key, long value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public boolean save(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        try {
            editor.putString(key, value);
            return editor.commit();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return false;
    }

    public boolean save(String key, boolean value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    @SuppressLint("NewApi")
    public boolean save(String key, Set<String> values) {

        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putStringSet(key, values);
        return editor.commit();

    }

    public boolean isContain(String key) {

        return mSharedPreferences.contains(key);
    }

    public SharedPreferences getSharedPreferences() {
        return mSharedPreferences;
    }

}